<?php

namespace addons\qrcode\controller;
namespace app\admin\controller\Train;

use think\addons\Controller;
use think\Response;
use Endroid\QrCode\QrCode;
use app\common\controller\Backend;


/**
 * 教室管理
 *
 * @icon fa fa-users
 */
class Trainall extends Backend
{

    /**
     * @var \app\admin\model\UserArea
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Trainall');
        $this->view->assign("statusList", $this->model->getStatusList());
    }
	
	public function import(){
		 return parent::import();
	}

    public function add()
    {
        $nodeList = \app\admin\model\UserRule::getTreeList();
        $this->assign("nodeList", $nodeList);
        return parent::add();
    }

    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
		$rules = explode(',', $row['rules']);
        $nodeList = \app\admin\model\UserRule::getTreeList($rules);
		
		/**
		$text = $this->request->get('text', 'hello world');
        $size = $this->request->get('size', 250);
        $padding = $this->request->get('padding', 15);
        $errorcorrection = $this->request->get('errorcorrection', 'medium');
        $foreground = $this->request->get('foreground', "#ffffff");
        $background = $this->request->get('background', "#000000");
        $logo = $this->request->get('logo');
        $logosize = $this->request->get('logosize');
        $label = $this->request->get('label');
        $labelfontsize = $this->request->get('labelfontsize');
        $labelhalign = $this->request->get('labelhalign');
        $labelvalign = $this->request->get('labelvalign');

        // 前景色
        list($r, $g, $b) = sscanf($foreground, "#%02x%02x%02x");
        $foregroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];

        // 背景色
        list($r, $g, $b) = sscanf($background, "#%02x%02x%02x");
        $backgroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];

        $qrCode = new QrCode();
        $qrCode
            ->setText($text)
            ->setSize($size)
            ->setPadding($padding)
            ->setErrorCorrection($errorcorrection)
            ->setForegroundColor($foregroundcolor)
            ->setBackgroundColor($backgroundcolor)
            ->setLogoSize($logosize)
            ->setLabel($label)
            ->setLabelFontSize($labelfontsize)
            ->setLabelHalign($labelhalign)
            ->setLabelValign($labelvalign)
            ->setImageType(QrCode::IMAGE_TYPE_PNG);
        $fontPath = ROOT_PATH . 'public/assets/fonts/SourceHanSansK-Regular.ttf';
        if (file_exists($fontPath)) {
            $qrCode->setLabelFontPath($fontPath);
        }
        if ($logo) {
            $qrCode->setLogo(ROOT_PATH . 'public/assets/img/qrcode.png');
        }
        //也可以直接使用render方法输出结果
        //$qrCode->render();
		//$png = new Response($qrCode->get(), 200, ['Content-Type' => $qrCode->getContentType()]);
		$png = $qrCode->get('png');
		//echo $png;
		**/
		//$this->Messagemodel = model('Message');
       // $MessageList = $this->Messagemodel
		//->column("id,name,category,num,typename,typerules,status");
		//->select();
		//print_r($MessageList);
        //$this->view->assign("MessageList", $MessageList);
		
		
		$this->assign("nodeList", $nodeList);

        return parent::edit($ids);
    }
	
	 public function Messagelist()
    {
        if ($this->request->isAjax()) {
			$this->Messagemodel = model('Message');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null);
            $total = $this->Messagemodel
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->Messagemodel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
	
	 public function Messagelistlog()
    {
        if ($this->request->isAjax()) {
			$this->Messagemodel = model('Messagelog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null);
            $total = $this->Messagemodel
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->Messagemodel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
	


}
